
function matchPeople(text) {
    const regex4 = /<strong>\s*<a(\s+|\w+)[^>]*>([^<]+)<\/a>\s*<\/strong>/gi;
    const matches = [...text.matchAll(regex4)];
    return matches.map((m) => m[2])
}

function matchPeople2(text) {
    // 100%有效的正则表达式
    const regex = /<li[^>]*><a[^>]*?class=['"]text['"][^>]*>([^<]+)<\/a><\/li>/gi;
    const matches = [...text.matchAll(regex)];
    return matches.map(match => match[1]);
}

/**
 * 
 * @param {string} page 页码
 * @param {string} keyword 关键字 （all 代表全部）
 * @param {string} peopleType 歌手类型
 * @returns 
 */
function findMusicPeople(page, keyword, peopleType) {
  return fetch(`https://www.kugou.com/yy/singer/index/${page}-${keyword}-${peopleType}.html`, {
    headers: {
      accept:
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
      "accept-language": "zh-CN,zh;q=0.9",
      "cache-control": "no-cache",
      pragma: "no-cache",
      priority: "u=0, i",
      "sec-ch-ua":
        '"Google Chrome";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": '"macOS"',
      "sec-fetch-dest": "document",
      "sec-fetch-mode": "navigate",
      "sec-fetch-site": "same-origin",
      "sec-fetch-user": "?1",
      "upgrade-insecure-requests": "1",
    },
    referrer: `https://www.kugou.com/yy/singer/index/${page}-${keyword}-${peopleType}.html`,
    referrerPolicy: "strict-origin-when-cross-origin",
    body: null,
    method: "GET",
    mode: "cors",
    credentials: "include",
  })
    .then((e) => e.text())
    .then((e) => {
        const text = e.replace(/\s+/g, '').replace(/(\\x3C!--|<!--)[\s\S]*?(--\\x3E|-->)/g, '')
        const people = matchPeople(text)
        const people2 = matchPeople2(text)
        return [...people, ...people2]
    });
}

// 正则表达式匹配是否包含中文
function isNonChinese(text) {
    const chineseRegex = /[\u4e00-\u9fff\u3400-\u4dbf]/;
    return !chineseRegex.test(text); // 如果不包含中文，返回 true
}

function findAllMusicPeople(keywords, peopleType) {
   const p = keywords.map(keyword => {
     return  Array.from({ length: 5 }, (_, i) => findMusicPeople(i + 1, keyword, peopleType))
    }).reduce((acc, cur) => {
        return [...acc, ...cur]
    }, [])
    return Promise.allSettled(p).then(e => {
        const last = e.filter(t => t.value).map(v => v.value)
        const all = last.reduce((acc, cur) => {
            return [...acc, ...cur]
        }, [])
        return all.filter((j)=>{
            if(isNonChinese(j) && j.length <=3) {
                return false;
            }
            return true
        })
    })
}

const PEOPLE_TYPE = [
    {
        keyword: '全部歌手',
        type: '1'
    },
    {
        keyword: '华语男歌手',
        type: '2'
    },
    {
        keyword: '华语女歌手',
        type: '3'
    },
    {
        keyword: '华语组合',
        type: '4'
    },
    {
        keyword: '日韩男歌手',
        type: '5'
    },
    {
        keyword: '日韩女歌手',
        type: '6'
    },
    {
        keyword: '日韩组合',
        type: '7'
    },
    {
        keyword: '欧美男歌手',
        type: '8'
    },
    {
        keyword: '欧美女歌手',
        type: '9'
    },
    {
        keyword: '欧美组合',
        type: '10'
    },
    {
        keyword: '其他',
        type: '11'
    },
    
]

/**
 *  在 https://www.kugou.com/yy/singer/index/1-b-2.html 这个域名里执行当前代码
 * @param {string} keyword 关键字 （all 代表全部） 否则对应字符开头
 * @param {string} peopleType 歌手类型 对应上面的 PEOPLE_TYPE
 * @returns 
 */
findAllMusicPeople(['m','n','o','p','q','r','s','t','u','v','w','x','y','z'], '11').then(e => {
   const newPeople = e.filter((k) => !window.Had_PEOPLES.includes(k))
   window.Had_PEOPLES = [...window.Had_PEOPLES, ...newPeople];
   window.no_load_peoples = [...(window.no_load_peoples || []), ...newPeople];
   console.log(window.no_load_peoples)
})